import matplotlib.pyplot as plt

# 训练集
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

# 初始化权重
w = 1.0

# 定义线性拟合函数
def forward(x):
    return x * w

# 定义损失函数
def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) ** 2

# 定义梯度函数
def gradient(x, y):
    return 2 * x * (x * w - y)

# 存储迭代次数和损失函数
epoch_list = []
loss_list = []

print('predict (before training)：','x = 4','y=',forward(4)) #训练之前：预测

# 随机梯度下降算法（stochastic gradient descent）
# 每个epoch都使用三个样本分别进行参数更新，一共更新100*3=300次
for epoch in range(100):
    for x, y in zip(x_data, y_data):
        grad = gradient(x, y)
        w = w - 0.05 * grad  # 通过训练集中每个样本的梯度进行参数更新
        print("\t grad:", x, y, grad)
        l = loss(x, y)
    print("progress:", epoch, "w=", w, "loss=", l)
    epoch_list.append(epoch)
    loss_list.append(l)

print('predict (after training)：','x = 4','y =',forward(4)) #训练之后：预测

# 可视化
plt.plot(epoch_list, loss_list)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()